<template>
	<view class="container">
		<view class="visitor">
			<view class="outside">
				<view class="within">
					<view class="info">
						<view class="name">
							<text>{{infoData.name}}</text>
							<!-- <text class="classType">【门诊】</text> -->
							<view class="btn m-l-30" @click="showVisitCard=true">
								<img src="@/static/images/qiehuan.png" style="width:29rpx;margin-right:8rpx;"></img>
								切换就诊人
							</view>
						</view>
						<view class="cord">
							卡号
						</view>
						<view>{{$desensitizationNormalStr(infoData.idNum)}}</view>
					</view>
				</view>
			</view>
		</view>
		<view class="screenType">
			<!-- <view class="typeLeft" @click="showpop">
			{{timesRange}}
			<u-icon name="arrow-down-fill" color="#fff" size="10"></u-icon>
		</view> -->
			<view class="typeRight" @click="showTypes">
				{{typesName}}
				<u-icon name="arrow-down-fill" color="#fff" size="10"></u-icon>
			</view>
		</view>
		<view class="listWrap">
			<view class="noCard" v-if="dataList.length<1">
				<view class="images">
					<img src="@/static/images/noCard.png" alt="">
					<view>
						未查询到信息
					</view>
				</view>
			</view>
			<view class="list" v-else>
				<scroll-view class="scroll-view_H" scroll-y="true" style="height: calc(100vh - 345rpx);">
					<view class="items" v-for="(item,index) in dataList" :key="index">
						<view class="title">
							<text>{{item.reservType == '1'?'预约信息':'挂号信息'}}</text>
							<view class="btn">
								{{item.orderStatu|orderStatu}}
							</view>
						</view>
						<view class="time">
							<text class="left">就诊科室</text>
							<text>{{item.deptName}}</text>
						</view>
						<view class="icd">
							<text class="left">{{item.reservType == '1'?'预约医生':'挂号医生'}}</text>
							<text>{{item.doctorName}}|{{item.scheduleType}}</text>
						</view>
						<view class="name">
							<text class="left">就诊日期</text>
							<text>{{item.scheduleDate}}</text>
						</view>
						<view class="name">
							<text class="left">就诊时间</text>
							<text>{{item.timeRange}}</text>
						</view>
						<view class="dept">
							<text class="left">就诊医院</text>
							<text>{{hospitalName}}</text>
						</view>
						<view class="dept">
							<text class="left">挂号费</text>
							<text>￥{{item.totalAmount}}</text>
						</view>
						<view class="btns" v-if="item.orderStatu=='0'">
							<u-button class="m-t-15 m-b-15" style="width: 45%;"
								@click="cancelReservation(item)">{{item.reservType == '1'?'取消预约':'取消挂号'}}</u-button>
							<!-- <u-button v-if="new Date().getTime() > new Date(item.scheduleDate+' 08:00:00').getTime()&&new Date().getTime() < new Date(item.scheduleDate+' '+item.timeRange.substring(6)+':00').getTime()" @click="confirmReservation(item)" class="m-t-15 m-b-15" type="primary" style="width: 45%;">预约签到</u-button> -->
							<!-- <u-button @click="confirmReservation(item)" class="m-t-15 m-b-15" type="primary"
								style="width: 45%;">{{item.reservType == '1'?'预约签到':'支付'}}
							</u-button> -->
							
							<!-- 2025-02-27是否启用预约挂号先支付模式1:启用 其他：不启用 -->
							<u-button v-if="item.reservType == '1'" @click="confirmReservation(item)" class="m-t-15 m-b-15" type="primary"
								style="width: 45%;">{{yyzf == '1'?'支付':'预约签到'}}
							</u-button>
							<u-button v-if="item.reservType == '2'" @click="confirmReservation(item)" class="m-t-15 m-b-15" type="primary"
								style="width: 45%;">支付
							</u-button>
						</view>
						<!-- <view class="btns" v-if="item.orderStatu=='7'">
							<text>已签到</text>
						</view> -->
						<!-- 2025-02-27是否启用预约挂号先支付模式1:启用 其他：不启用 -->
						<view class="btns" v-if="item.orderStatu=='1' && yyzf == '1'">
							<u-button @click="confirmReservation(item)" class="m-t-15 m-b-15" type="primary"
								style="width: 45%;">预约签到
							</u-button>
						</view>
						
					</view>
				</scroll-view>
			</view>
		</view>
		<u-picker keyName="label" :show="showType" @cancel='showType=false' :closeOnClickOverlay='true'
			@close="showType=false" :columns="columns" @confirm="confirm"></u-picker>
		<liuCalendarDate ref="calendar2" isRange @checked='changeTime'></liuCalendarDate>
		<!-- 切换就诊人 -->
		<u-popup :show="showVisitCard" :round="10" :safeAreaInsetBottom="true" mode="bottom">
			<switchVisitCard :visitData="visitList" :relations="relationTrans" @closeVisit="close" />
		</u-popup>
	</view>
</template>
<script src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
<script>
	import { getConfigKey } from "@/api/home/home.js";
	import {
		getPatientBaseList
	} from "@/api/card/index.js"
	import switchVisitCard from '@/components/switchVisitCard/index.vue';
	import liuCalendarDate from '@/components/liu-calendar-date/liu-calendar-date.vue';
	import {
		getDictType
	} from "@/api/system/public.js";
	import {
		getReservationList,
		cancelReservation,
		payUnifiedOrder,
		checkReservation,
		checkReservationToday
	} from "@/api/pay/index.js";
	import {
		encrypt,
		decrypt
	} from '@/utils/sm4Util.js'
	import {
		EventBus
	} from "@/utils/event-bus.js";
	export default {
		components: {
			switchVisitCard,
			liuCalendarDate
		},
		data() {
			return {
				isRequest:false,
				yyzf:'',
				infoData: {},
				timesRange: '全部时间',
				typesName: '全部',
				showTime: false,
				visitList: [],
				showVisitCard: false,
				relationTrans: {},
				showType: false,
				dataList: [],
				startTime: '',
				endTime: '',
				checkType: '',
				hospitalName: '',
				columns: [
					[{
						label: '全部',
						id: ''
					}, {
						label: '未支付',
						id: '0'
					}, {
						label: '支付成功',
						id: '1'
					}, {
						label: '退款',
						id: '2'
					}, {
						label: '已取消',
						id: '3'
					}, {
						label: '预约失败',
						id: '6'
					}]
				],
				openid: '',
			}
		},
		computed: {
			getDisbal: function() {
				return function(val) {
					if (new Date().getTime() > new Date(val + ' 08:00:00').getTime()) {
						return true
					} else {
						return false
					}
				}
			}
		},
		filters: {
			orderStatu(val) {
				//订单状态（0：未支付 1:支付成功 2：退款 3：取消
				if (val == 0) {
					return '未支付'
				} else if (val == 1) {
					return '支付成功'
				} else if (val == 2) {
					return '退款'
				} else if (val == 3) {
					return '取消'
				} else if (val == 6) {
					return '预约失败'
				}else if (val == 7) {
					return '签到完成'
				}
			}
		},
		onLoad(options) {
			let openid = uni.getStorageSync('openid') || ''
			this.openid = openid
			if (uni.getStorageSync('currentVisitCard')) {
				this.infoData = JSON.parse(uni.getStorageSync('currentVisitCard'))
			}
			this.hospitalName = JSON.parse(uni.getStorageSync('hospital')).name
			this.getConfig();
			this.getCardList()
			this.getDict()
			this.getReservationList()

		},
		onPullDownRefresh() {
			this.getReservationList()
		},
		methods: {
			/* 获取参数配置 */
			getConfig() {
				getConfigKey('yyzf').then(res => {
					this.yyzf = res.msg;
				});
			},
			getReservationList() {
				getReservationList({
					hospitalCode: JSON.parse(uni.getStorageSync('hospital')).hospitalCode,
					cardNum: JSON.parse(uni.getStorageSync('currentVisitCard')).cardNum,
					orderStatu: this.checkType,
					openId: this.openid
				}).then(res => {
					//数据请求完成之后停止下拉刷新
					uni.stopPullDownRefresh();
					this.dataList = res.rows
				})
			},
			/* 就诊人列表 */
			getCardList() {
				let params = {
					hospitalCode: JSON.parse(uni.getStorageSync('hospital')).hospitalCode,
					openId: this.openid,
				}
				getPatientBaseList(params).then(res => {
					if (res.code == 200) {
						this.visitList = res.rows
					}
				})
			},
			/* 取消挂号&预约 */
			cancelReservation(item) {
				item.orderStatu = '3'
				cancelReservation(item).then(res => {
					if (res.code == 200) {
						this.$msg("取消成功");
						this.getReservationList()
					}
				})
			},
			/* 支付 */
			confirmReservation(item) {
				let _that = this
				let names = JSON.parse(uni.getStorageSync('currentVisitCard')).name
				if (item.reservType == '2') {
					// 当日挂号支付
					uni.navigateTo({
						url: '/pages/pay/index'
					})
				} else if(item.reservType == '1'){
					// 预约挂号
					if(this.yyzf == '1' && item.orderStatu=='0'){
						let obj = {
							"hospitalCode": JSON.parse(uni.getStorageSync('hospital'))
								.hospitalCode, //医院微信唯一标识
							"openId": _that.openid, //微信唯一标识
							"bh": JSON.parse(uni.getStorageSync('currentVisitCard')).idNum, //住院号/卡号
							"recipelNum": item.id, //处方号（门诊业务专用字段
							"orderAmount": item.totalAmount, //订单金额
							"orderDesc": item.reservType == '1' ? '预约挂号-' + names : '当班挂号-' +
								names, //订单描述（医卡通，预约，门诊支付 住院押金）
							"payType": "weChat", //支付类型（weChat:微信 Alipay:支付宝）
							"orderType": "2" //订单类型（1医卡通，2预约，3门诊支付 4住院押金）
						}
						let param = {
							params: encrypt(JSON.stringify(obj))
						}
						payUnifiedOrder(param).then(res => {
							WeixinJSBridge.invoke('getBrandWCPayRequest', {
									"appId": res.data.appId,
									"timeStamp": res.data.timeStamp,
									"nonceStr": res.data.nonceStr,
									"package": res.data.package,
									"signType": "MD5",
									"paySign": res.data.paySign,
								},
								function(res) { // 调起微信支付成功
									if (res.err_msg == "get_brand_wcpay_request:ok") {
										_that.$msg("支付成功!");
										_that.typesName = '支付成功'
										_that.checkType = 1
										_that.getReservationList()
									} else if (res.err_msg == "get_brand_wcpay_request:cancel") {
										_that.$msg("用户取消支付!");
									} else {
										_that.$msg("支付失败,请重新支付!" + res.err_msg);
									}
								});
						
						})
						
					}else if(this.yyzf == '1' && item.orderStatu=='1'){
						if(this.isRequest==true){
							return;
						}
						this.isRequest=true;
						checkReservation(item.id).then(ress => {
							if (ress.code == 200) {
								_that.$msg("挂号成功!");
								_that.getReservationList()
							}
						}).finally(e=>{
							this.isRequest=false;
						})
					}else{
						checkReservation(item.id).then(ress => {
							if (ress.code == 200) {
								let obj = {
									"hospitalCode": JSON.parse(uni.getStorageSync('hospital'))
										.hospitalCode, //医院微信唯一标识
									"openId": _that.openid, //微信唯一标识
									"bh": JSON.parse(uni.getStorageSync('currentVisitCard')).idNum, //住院号/卡号
									"recipelNum": item.id, //处方号（门诊业务专用字段
									"orderAmount": item.totalAmount, //订单金额
									"orderDesc": item.reservType == '1' ? '预约挂号-' + names : '当班挂号-' +
										names, //订单描述（医卡通，预约，门诊支付 住院押金）
									"payType": "weChat", //支付类型（weChat:微信 Alipay:支付宝）
									"orderType": "2" //订单类型（1医卡通，2预约，3门诊支付 4住院押金）
								}
								let param = {
									params: encrypt(JSON.stringify(obj))
								}
								payUnifiedOrder(param).then(res => {
									WeixinJSBridge.invoke('getBrandWCPayRequest', {
											"appId": res.data.appId,
											"timeStamp": res.data.timeStamp,
											"nonceStr": res.data.nonceStr,
											"package": res.data.package,
											"signType": "MD5",
											"paySign": res.data.paySign,
										},
										function(res) { // 调起微信支付成功
											if (res.err_msg == "get_brand_wcpay_request:ok") {
												_that.$msg("支付成功!");
												_that.typesName = '支付成功'
												_that.checkType = 1
												_that.getReservationList()
											} else if (res.err_msg == "get_brand_wcpay_request:cancel") {
												_that.$msg("用户取消支付!");
											} else {
												_that.$msg("支付失败,请重新支付!" + res.err_msg);
											}
										});
								
								})
							}
						})
					}
					
				}
			},
			close() {
				this.showVisitCard = false
				this.infoData = JSON.parse(uni.getStorageSync('currentVisitCard'))
				EventBus.$emit('loadVisitor', '')
				this.getReservationList()
			},
			confirm(e) {
				this.typesName = e.value[0].label
				this.checkType = e.value[0].id
				this.getReservationList()
				this.showType = false
			},
			showTypes() {
				this.showType = true
			},
			showpop() {
				this.$refs.calendar2.open()
			},
			changeTime(time) {
				this.timesRange = time.value[0] + '至' + time.value[1]
				this.startTime = time.value[0]
				this.endTime = time.value[1]
			},
			/* 字典 */
			getDict() {
				getDictType({
					dictType: 'sys_relation'
				}).then(res => {
					console.log(res)
					if (res.code == 200) {
						res.rows.forEach(item => {
							this.relationTrans[item.dictValue] = item.dictLabel
						})
					}
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	page {
		background-color: rgb(245, 245, 245);
	}

	.listWrap {
		width: 100%;
		// background: url('../../../static/images/oneDay.png') no-repeat;
		// background-size: 100% 100%;
		position: relative;

		.noCard {
			width: 100%;
			text-align: center;
			color: #666666;
			padding: 24rpx 0 0 0;
			margin-top: 72rpx;
			color: #808080;

			.images {
				width: 100%;
				margin-bottom: 40rpx;
				color: #666666;

				img {
					width: 55%;
					text-align: center;
				}
			}
		}

		.list {
			padding: 24rpx;
			margin: 0 auto;

			.items {
				background-color: #fff;
				border-radius: 8px;
				margin-bottom: 24rpx;
				padding-bottom: 12rpx;

				.title {
					padding: 24rpx;
					display: flex;
					font-size: 32rpx;
					font-weight: 600;
					align-items: center;
					border-bottom: 1rpx solid #eee;
					justify-content: space-between;

					.btn {
						font-size: 24rpx;
						border: 1rpx solid #D2E3FF;
						font-weight: 400;
						border-radius: 4px;
						margin-left: 24rpx;
						padding: 4rpx 9rpx;
						background-color: #D2E3FF;
						color: rgb(92, 147, 249);
					}
				}

				.name,
				.icd,
				.time,
				.dept {
					display: flex;
					justify-content: space-between;
					padding: 12rpx 24rpx;
					font-size: 28rpx;

					.left {
						color: #999;
						width: 130rpx;
					}

					.icdtext {
						flex: 1;
						color: #FF6430;
						text-align: right;
					}
				}

				.btns {
					display: flex;
					align-items: center;
				}
			}
		}
	}

	.screenType {
		width: 100%;
		display: flex;
		align-items: center;
		padding: 20rpx;
		background-color: rgb(92, 147, 249);
		justify-content: space-around;
		color: #fff;

		.typeLeft,
		.typeRight {
			display: flex;
			align-items: center;
		}
	}

	.visitor {
		width: 100%;
		// background-color: rgb(92, 147, 249);
		background: url('@/static/images/report-small-bg.png') no-repeat left center, linear-gradient(180deg, rgba(197, 218, 255, 1) 0%, rgba(92, 147, 249, 1) 100%);
		background-size: auto 100%;
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 24rpx 24rpx 0 24rpx;
		color: #3C537D;

		.outside {
			width: 100%;
			background-color: #fff;
			padding: 12rpx 12rpx 0 12rpx;
			border-radius: 8px 8px 0 0;
			background: url('@/static/images/card_bg.png') no-repeat;
			background-size: 100% 100%;

			.within {
				border-radius: 8px 8px 0 0;

				.info {
					padding: 16rpx 28rpx 20rpx;
					font-size: 34rpx;

					.name {
						display: flex;
						align-items: center;

						.classType {
							display: inline-block;
							margin: 0 36rpx;
						}

						.btn {
							font-size: 28rpx;
							padding: 7rpx 14rpx;
							background: linear-gradient(90deg, rgba(77, 142, 255, 1) 0%, rgba(94, 150, 247, 1) 100%);
							color: #fff;
							display: flex;
							align-items: center;
							border-radius: 5px;
						}
					}

					.cord {
						font-size: 28rpx;
						padding: 16rpx 0 8rpx;
					}
				}
			}
		}
	}
</style>